<?php
namespace app\hotback\controller;
use think\Db;
class Service extends Base
{
	public function _initialize()
	{
		// 调用父类前置方法
		parent::_initialize();

		// 登录校验
		$this->IsLogin();
	}

	/**
	 * 服务列表
	 */
	public function index()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		if(request()->isPost())
		{
			$params = input();
			if(!empty($params['page']) && !empty($params['limit']))
			{
				$map = [
					'is_delete_time' => 0
				];
				if(!empty($params['title']))
				{
					$map['title'] = ['like','%'.$params['title'].'%'];
				}
				$count = Db::name('service')->where($map)->count();
				$list_data = Db::name('service')->where($map)->order('id desc')->limit(($params['page']-1)*$params['limit'],$params['limit'])->select();
				if(!empty($list_data))
				{
					//遍历处理
					foreach($list_data as $key => $value)
					{
						//编辑url
						$list_data[$key]['save_url'] = url('Service/saveinfo', ['id' => $value['id']]);
					}
					$result['code'] = 0;
	        		$result['msg'] = "请求成功";
	        		$result['data'] = $list_data;
	        		$result['count'] = $count;
				}else
				{
					$result['code'] = -1;
	        		$result['msg'] = "暂无数据";
				}
			}
			echo json_encode($result);
		}else
		{
			return $this->fetch();
		}
	}

	/**
	 * 服添添加/修改 页面
	 */
	public function saveinfo()
	{
		// 参数
		$params = input();
		$info = [];
		if(!empty($params['id']))
		{
			//查询
			$info = Db::name('service')->where(['id' => $params['id']])->find();
		}
		$this->assign('info',$info);

		//编辑器文件存放地址
		$this->assign('editor_path_type', 'service');
		
		$service_class=db::name('service_class')->where('is_delete_time',0)->select();
		
		$this->assign('service_class',$service_class);
		
		return $this->fetch();
	}

	/**
	 * 服务添加/修改
	 */
	public function save()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		if(request()->isPost())
		{
			$params = input();
			$validate = $this->validate(
				$params,
				[
					'title' => 'require|unique:service,is_delete_time=0&title='.$params['title'],
					'images' => 'require',
					'carousels' => 'require',
					'price' => ['require', 'regex' => '^([0-9]{1}\d{0,6})(\.\d{1,2})?$', 'egt' => 0.01],
					'original_price' => ['require', 'regex' => '^([0-9]{1}\d{0,6})(\.\d{1,2})?$'],
					'sales_unreal' => 'integer|egt:0',
					'duration' => 'require|integer|egt:1',
					'introduce' => 'require',
					'taboos' => 'require',
					'notice' => 'require'
				],
				[
					'title.require' => '请输入服务名称',
					'title.unique' => '名称已存在',
					'images.require' => '请上传封面图',
					'carousels.require' => '请上传轮播图',
					'price.require' => '请输入服务价格',
					'price.regex' => '服务价格有误',
					'price.egt' => '服务价格大于等于0.01',
					'original_price.require' => '请输入服务原价',
					'original_price.regex' => '服务原价有误',
					'sales_unreal.integer' => '虚拟销量为整数',
					'sales_unreal.egt' => '虚拟销量大于等于0',
					'duration.require' => '请输入服务时长',
					'duration.integer' => '服务时长为整数',
					'duration.egt' => '服务时长大于等于1',
					'introduce.require' => '请输入项目介绍',
					'taboos.require' => '请输入禁忌说明',
					'notice.require' => '请输入下单须知'
				]
			);
			if(true !== $validate)
			{
				echo json_encode(['code' => -1, 'msg' => $validate]);
				exit();
			}
			//基础数据
			$data = [
				'title' => $params['title'],
				'sub_title' => $params['sub_title'],
				'class_id' => $params['service_id'],
				'images' => $params['images'],
				'carousels' => implode(",", $params['carousels']),
				'price' => $params['price'],
				'original_price' => $params['original_price'],
				'sales_unreal' => intval($params['sales_unreal']),
				'sales_total' => Db::raw('sales_actual+'.intval($params['sales_unreal'])),
				'duration' => $params['duration'],
				'introduce' => $params['introduce'],
				'taboos' => $params['taboos'],
				'notice' => $params['notice'],
				'is_shelves' => isset($params['is_shelves']) ? intval($params['is_shelves']) : 0,
				'sort' => isset($params['sort']) ? intval($params['sort']) : 0
			];
			// 添加/编辑
			if(empty($params['id']))
			{
				$data['add_time'] = time();
        		$data['upd_time'] = time();
        		$service_id = Db::name('service')->insertGetId($data);
			}else
			{
				$data['upd_time'] = time();
				if(Db::name('service')->where(['id'=>intval($params['id'])])->update($data))
				{
					$service_id = $params['id'];
				}
			}
			//是否成功
			if(isset($service_id) && $service_id > 0)
			{
				$result['code'] = 0;
				$result['msg'] = "操作成功";
			}else
			{
				$result['code'] = -1;
				$result['msg'] = "操作失败";
			}
		}
		echo json_encode($result);
	}

	/**
	 * 服务删除
	 */
	public function delete()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		if(request()->isPost())
		{
			$params = input();
			$data = [
				'is_delete_time' => time(),
				'upd_time' => time()
			];
			if(Db::name('service')->where(['id' => intval($params['id'])])->update($data))
			{
				$result['code'] = 0;
				$result['msg'] = "删除成功";
			}else
			{
				$result['code'] = -1;
				$result['msg'] = "删除失败";
			}
		}
		echo json_encode($result);
	}
}